#include "qdefaultkeyboardmanager.h"

#include "xmlobject.h"
#include "qhostfactory.h"
#include "host/qabstracthost.h"
#include "host/qabstractwidgethost.h"


#include <QFile>

QDefaultKeyBoardManager::QDefaultKeyBoardManager(QObject *parent) :
    QObject(parent)
{
    m_files<<":/files/byte_keyboard.xml";
    m_files<<":/files/Number_Keyboard.xml";
}

QDefaultKeyBoardManager::~QDefaultKeyBoardManager()
{
    clear();
}

void QDefaultKeyBoardManager::clear()
{
    qDeleteAll(m_name_to_keyboard.values());
    m_files.clear();
    m_name_to_keyboard.clear();
}

void QDefaultKeyBoardManager::load()
{
    QAbstractHost *host;

    foreach(const QString file,m_files)
    {
        XMLObject xml;
        QFile f(file);
        if(!f.open(QFile::ReadOnly))
        {
            continue;
        }
        QString str=f.readAll();
        if(!xml.load(str,0))
        {
            continue;
        }
        host=QHostFactory::create_host(&xml);
        if(host!=NULL)
        {
            ((QAbstractWidgetHost*)host)->refresh_level();
            host->set_default();
            host->make_stylesheet();
            m_name_to_keyboard.insert(host->get_uuid(),host);
        }
    }
}

QMap<QString,QAbstractHost*> QDefaultKeyBoardManager::get_all_keyboards()
{
    return m_name_to_keyboard;
}

QAbstractHost* QDefaultKeyBoardManager::get_keyboard(const QString &name)
{
    return m_name_to_keyboard.value(name);
}
